﻿/**********************************天使物流*******************************************
* 功能描述:基本信息-油费
* 作者:李周蓬
* 时间:
* 
**********************************天使物流*******************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.BaseInfo.OilCosts;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.BaseInfo.OilCosts
{
    public class OilCostRepository : DapperDbContextRepository, IOilCostRepository
    {
        public OilCostRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示油费管理
        /// </summary>
        /// <param name="cardname"></param>
        /// <param name="name"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<OilCostDto>> GetShowOilCost(string cardname, string name, int pageindex, int pagesize)
        {
            PageResponse<OilCostDto> response = new PageResponse<OilCostDto>();
            var sql = $"select * from OilCost a join Dictionary b on a.PayTypeId=b.Id where a.IsDelete=0 ";
            if (!string.IsNullOrWhiteSpace(cardname))
            {
                sql += $" and a.LicenseNumber='{cardname}'";
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                sql += $" and a.Agent like '%" + name + "%'";
            }
            var list = await this.QueryAsync<OilCostDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<OilCostDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除油费管理
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelOilCost(int id)
        {
            var sql = $"update OilCost set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除油费管理
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsOilCost(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update OilCost set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="OilCostDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateOilCost(OilCostDto oilCostDto)
        {
            var sql = $"update OilCost set LicenseNumber='{oilCostDto.LicenseNumber}',ComeCost='{oilCostDto.ComeCost}',FuelCharge='{oilCostDto.FuelCharge}',BeginKM='{oilCostDto.BeginKM}',PayTypeId='{oilCostDto.PayTypeId}',Remark='{oilCostDto.Remark}' where Id={oilCostDto.Id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加油费管理
        /// </summary>
        /// <param name="OilCostDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddOilCost(OilCostDto oilCostDto)
        {
            var sql = $"insert into OilCost values('{oilCostDto.OilCostNumber}','{oilCostDto.LicenseNumber}',{oilCostDto.ComeCost},{oilCostDto.FuelCharge},{oilCostDto.BeginKM},{oilCostDto.PayTypeId},'{oilCostDto.Agent}','{oilCostDto.Remark}',GETDATE(),0)";
            var list = await ExecuteAsync
               (sql, oilCostDto);
            return list;
        }
        /// <summary>
        /// 绑定
        /// </summary>
        /// <returns></returns>
        public async Task<PageResponse<DictionaryDto>> GetBind()
        {
            PageResponse<DictionaryDto> response = new PageResponse<DictionaryDto>();
            var sql = "select * from Dictionary";
            response.PageData= await this.QueryAsync<DictionaryDto>(sql,null);
            return response;
        }
    }
}
